//
//  SetPwdViewController.m
//  moon
//
//  Created by beyself on 16/6/25.
//  Copyright © 2016年 beyself. All rights reserved.
//

#import "SetPwdViewController.h"
#import "Utils.h"
#import "SmsCodeViewController.h"
#import "LoginViewController.h"

@interface SetPwdViewController ()

@end

@implementation SetPwdViewController

- (void)viewDidLoad {
    [super viewDidLoad];    
    self.title = @"设置密码";
    for(int i = 0; i < 2; i++)
    {
        UIView* subview = [[UIView alloc] init];
        subview.frame = CGRectMake(0, 20 + 49*i, SCREEN_WIDTH, 48);
        subview.backgroundColor = WHITE;
        
        UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
        button.frame = CGRectMake(0, 0, 48, 48);
        button.enabled = NO;
        [button setImage:[UIImage imageNamed:@"icon_code"] forState:UIControlStateDisabled];
        [subview addSubview:button];
        
        UITextField* textfield = [[UITextField alloc] init];
        textfield.tag = 1000 + i;
        textfield.frame = CGRectMake(48, 0, SCREEN_WIDTH - 58, 48);
        textfield.textColor = BLACK;
        textfield.keyboardType = UIKeyboardTypeEmailAddress;
        textfield.secureTextEntry = YES;
        if(i == 0)
            textfield.placeholder = @"请输入您要新设置的密码";
        else
            textfield.placeholder = @"请重复您刚才输入的密码";
        [subview addSubview:textfield];
        [self.view addSubview:subview];
    }
    
    UIButton* button = [UIButton buttonWithType:UIButtonTypeCustom];
    button.frame = CGRectMake(10, 140, SCREEN_WIDTH - 20, 44);
    button.layer.cornerRadius = 22;
    button.layer.masksToBounds = YES;
    button.titleLabel.font = FONT_15;
    [button addTarget:self action:@selector(loginBtn) forControlEvents:UIControlEventTouchUpInside];
    [button setBackgroundImage:[Utils imageFromColor:ORANGE] forState:UIControlStateNormal];
    [button setBackgroundImage:[Utils imageFromColor:LINE] forState:UIControlStateHighlighted];
    [button setTitle:@"完成" forState:UIControlStateNormal];
    [self.view addSubview:button];
}

- (void) showRegister
{
    SmsCodeViewController* subview = [[SmsCodeViewController alloc] init];
    [self.navigationController pushViewController:subview animated:YES];
}

- (void) loginBtn
{
    NSString* tel = ((UITextField*)[self.view viewWithTag:1000]).text;
    NSString* pwd = ((UITextField*)[self.view viewWithTag:1001]).text;
    
    if(tel.length == 0 || pwd.length == 0 || ![pwd isEqualToString:tel])
    {
        [self showMsg:@"请输入密码，并保持两次输入一致"];
        return;
    }
    
    NSMutableDictionary* param = [[NSMutableDictionary alloc] init];
    [param setObject:_mUseId forKey:@"id"];
    [param setObject:pwd forKey:@"password"];
    NSArray* array = [_mParam allKeys];
    for(NSString* str in array)
    {
        [param setObject:_mParam[str] forKey:[NSString stringWithFormat:@"identity[%@]",str]];
    }
    
    [Utils sendHttpPost:param method:@"member_active" controller:self flag:0];
}

- (void) httpCallback:(NSDictionary*)data flag:(int)flag
{
    [self showAllTextDialog:@"激活成功，请登录"];
    [self.navigationController popToRootViewControllerAnimated:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}
@end
